<?php
/**
 * 微信粉丝管理
 * User: Administrator
 * Date: 2019/11/27 0027
 * Time: 13:32
 */

namespace app\admin\controller;

use app\admin\traits\controller\WxFans;
use think\Db;
use think\Loader;
class WechatFans extends BasicAdmin{

    use WxFans;

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'WechatFans';

    //粉丝标签列表
    public function index(){
        if($this->request->isPost()){
            $param = $this->request->param();
            $db = Db::name($this->table)->order('id '.$param['order']);
            foreach (['nickname'] as $key) {
                (isset($param[$key]) && $param[$key] !== '') && $db->whereLike($key, "%{$param[$key]}%");
            }
//            (isset($param['status']) && $param['status'] !== '') && $db->where('status',$param['status']);
            $db->where('is_back',0); //去除黑名单用户
            return parent::_list($db,$param);
        }
        $this->title = '微信管理 > 微信粉丝管理';
        return $this->fetch('wechat/fans/index', ['title' => $this->title]);
    }

    /**
     * 黑名单列表
     */
    public function back(){
        if($this->request->isPost()){
            $param = $this->request->param();
            $db = Db::name($this->table)->where('is_back',1)->order('id '.$param['order']);
            foreach (['nickname'] as $key) {
                (isset($param[$key]) && $param[$key] !== '') && $db->whereLike($key, "%{$param[$key]}%");
            }
            return parent::_list($db,$param);
        }
        $this->title = '微信管理 > 微信粉丝管理';
        return $this->fetch('wechat/fans/back', ['title' => $this->title]);
    }

    /**
     * 粉丝标签选择
     */
    public function tagSet()
    {
        return $this->_form($this->table,'wechat/fans/tagset');
    }

    /**
     * 设置黑名单
     */
    public function backAdd()
    {
        if($this->request->isPost()){
            $param = $this->request->post();
            $rst = $this->_addBack($param);
            $ret = doReturn($rst['msg'],$rst['status'],[],'json');
            $this->resultArr($ret);
        }
    }

    /**
     * 移除黑名单
     */
    public function backDel(){
        if($this->request->isPost()){
            $param = $this->request->post();
            $rst = $this->_delBack($param);
            $ret = doReturn($rst['msg'],$rst['status'],[],'json');
            $this->resultArr($ret);
        }
    }

    //同步粉丝标签
    public function sync(){
        if($this->request->isPost()){
            $status = $this->_sync();
            $ret = doReturn('同步获取所有粉丝成功！','同步获取粉丝失败, 请稍候再试！',$status,[],'json');
            $this->resultArr($ret);
        }
    }

}